package com.css.fxfzcpxx.modules.earthquakeDisaster.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzcpxx.base.response.RestResponse;
import com.css.fxfzcpxx.constants.YNEnum;
import com.css.fxfzcpxx.modules.earthquakeDisaster.entity.EarthquakeDisaster;
import com.css.fxfzcpxx.modules.earthquakeDisaster.param.EarthquakeDisasterParam;
import com.css.fxfzcpxx.modules.earthquakeDisaster.service.EarthquakeDisasterService;
import com.css.fxfzcpxx.modules.earthquakeDisaster.vo.BasicDataSystemInfoVo;
import com.css.fxfzcpxx.modules.earthquakeDisaster.vo.EarthquakeDisasterVO;
import com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.param.FindByParam;
import com.css.fxfzcpxx.modules.websocket.WebSocketManager;
import com.css.fxfzcpxx.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/28
 * @Description: 地震灾害信息
 */
@Api(tags ="地震灾害信息")
@Slf4j
@RestController
@RequestMapping("/earthquakeDisaster")
public class EarthquakeDisasterController {
    @Resource
    private EarthquakeDisasterService earthquakeDisasterService;

    private final WebSocketManager webSocketManager;

    @Autowired
    public EarthquakeDisasterController(WebSocketManager webSocketManager) {
        this.webSocketManager = webSocketManager;
    }


    /**
     * 分页列表
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, EarthquakeDisasterParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = earthquakeDisasterService.getPage(curPage, pageSize,param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 发布
     */
    @PostMapping("/release/{id}")
    @Operation(summary = "发布")
    public RestResponse release(@PathVariable("id") List<String> Id) {
        try {
            earthquakeDisasterService.updateReleaseStatusById(Id, YNEnum.N.toString());
            webSocketManager.broadcastMessage("地震灾害信息发布啦","earthquakeDisaster");
            return RestResponse.succeed("发布成功");
        } catch (Exception e) {
            log.error("发布失败!", e);
            return RestResponse.fail("发布失败!");
        }
    }

    /**
     * 撤回
     */
    @PostMapping("/withdraw/{id}")
    @Operation(summary = "撤回")
    public RestResponse withdraw(@PathVariable("id") List<String> Id) {
        try {
            earthquakeDisasterService.updateReleaseStatusById(Id,YNEnum.Y.toString());
            webSocketManager.broadcastMessage("地震灾害信息撤回啦","earthquakeDisaster");
            return RestResponse.succeed("撤回成功");
        } catch (Exception e) {
            log.error("撤回失败!", e);
            return RestResponse.fail("撤回失败!");
        }
    }

    /**
     * 根据id查询
     * @param id 主键id
     * @return
     */
    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            EarthquakeDisasterVO data = earthquakeDisasterService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 删除单数据
     * @param Id 主键id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Operation(summary = "删除数据")
    public RestResponse deleteById(@PathVariable("id") String Id) {
        try {
            earthquakeDisasterService.deleteById(Id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 修改单条数据
     * @param param 参数
     * @return
     */
    @PostMapping("/updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody EarthquakeDisaster param) {
        RestResponse response = null;
        try {
            earthquakeDisasterService.updateById(param);
            response =RestResponse.succeed("修改成功");
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 目录下载
     * @param ids
     */
    @Operation(summary = "目录下载")
    @PostMapping("/exportData/{ids}")
    public void exportData(HttpServletResponse response,@PathVariable("ids") String ids) {
        earthquakeDisasterService.exportData(response, ids);
    }
    /***
     * 导入
     * @return
     */
    @PostMapping("/reportImport")
    public RestResponse reportImport(HttpServletRequest req){
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = earthquakeDisasterService.reportImport(inputStream);
            if (str.equals("导入成功")){
                response = RestResponse.succeed("",str);
            }else {
                response = RestResponse.fail(str);
            }
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /***
     * 对接基础数据系统历史震害数据
     * @return
     */
    @PostMapping("/dockingBasicDataSystem")
    public RestResponse dockingBasicDataSystem(BasicDataSystemInfoVo basicDataSystemInfoVo){
        RestResponse response = null;
        try{
            earthquakeDisasterService.dockingBasicDataSystem(basicDataSystemInfoVo);
            response = RestResponse.succeed();
            response.setMessage("保存成功");
        }catch (Exception e){
            String errorMessage = "保存失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     *
     * @return
     */
    @PostMapping("/basicDataUpdate")
    public RestResponse basicDataUpdate(BasicDataSystemInfoVo basicDataSystemInfoVo) {
        RestResponse response = null;
        try{
            earthquakeDisasterService.basicDataUpdate(basicDataSystemInfoVo);
            response = RestResponse.succeed();
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改成功!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
